/*-
 * Copyright (c) 2012-2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS TEST: tst_lv_1_dot.c    Rev. 0.0.3    2013-10-10T14:35+08:00 $
 */

#include <stdio.h>
#include <complex.h>

#include <cblas.h>

#include <tst_lv_a_com.h>

int test_dotf(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_ff_t vx;
    vec_ff_t vy;

    float buf_st;  /* Single-thread return */
    float buf_mt;  /* Multi-threads return */
    float buf_cf;  /* cblas return */

    ierr = vec_ff_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_ff_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_dotf(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_dotf(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);
    buf_cf = cblas_sdot(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step);

    printf("dotf() -> cblas_sdot()\n");
    check_value_ff_stdout("    ST:", buf_st);
    check_value_ff_stdout("    MT:", buf_mt);
    check_value_ff_stdout("    CF:", buf_cf);

    ierr = vec_ff_delete(&vx);
    ierr = vec_ff_delete(&vy);

    return ierr;
}


int test_dot(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_df_t vx;
    vec_df_t vy;

    double buf_st;  /* Single-thread return */
    double buf_mt;  /* Multi-threads return */
    double buf_cf;  /* cblas return */

    ierr = vec_df_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_df_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_dot(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_dot(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);
    buf_cf = cblas_ddot(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step);

    printf("dot() -> cblas_ddot()\n");
    check_value_df_stdout("    ST:", buf_st);
    check_value_df_stdout("    MT:", buf_mt);
    check_value_df_stdout("    CF:", buf_cf);

    ierr = vec_df_delete(&vx);
    ierr = vec_df_delete(&vy);

    return ierr;
}


int test_dotl(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_lf_t vx;
    vec_lf_t vy;

    long double buf_st;  /* Single-thread return */
    long double buf_mt;  /* Multi-threads return */

    ierr = vec_lf_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_lf_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_dotl(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_dotl(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);

    printf("dotl()\n");
    check_value_lf_stdout("    ST:", buf_st);
    check_value_lf_stdout("    MT:", buf_mt);

    ierr = vec_lf_delete(&vx);
    ierr = vec_lf_delete(&vy);

    return ierr;
}


int test_ucdotf(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_fc_t vx;
    vec_fc_t vy;

    float complex buf_st;  /* Single-thread return */
    float complex buf_mt;  /* Multi-threads return */
    float complex buf_cf;  /* cblas return */

    ierr = vec_fc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_fc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_ucdotf(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_ucdotf(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);
    cblas_cdotu_sub(PROPER_VECTOR_RUN, (void*) vx.data, vx.step, (void*) vy.data, vy.step, (void*) &buf_cf);

    printf("ucdotf() -> cblas_cdotu_sub()\n");
    check_value_fc_stdout("    ST:", buf_st);
    check_value_fc_stdout("    MT:", buf_mt);
    check_value_fc_stdout("    CF:", buf_cf);

    ierr = vec_fc_delete(&vx);
    ierr = vec_fc_delete(&vy);

    return ierr;
}


int test_ucdot(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_dc_t vx;
    vec_dc_t vy;

    double complex buf_st;  /* Single-thread return */
    double complex buf_mt;  /* Multi-threads return */
    double complex buf_cf;  /* cblas return */

    ierr = vec_dc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_dc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_ucdot(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_ucdot(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);
    cblas_zdotu_sub(PROPER_VECTOR_RUN, (void*) vx.data, vx.step, (void*) vy.data, vy.step, (void*) &buf_cf);

    printf("ucdot() -> cblas_zdotu_sub()\n");
    check_value_dc_stdout("    ST:", buf_st);
    check_value_dc_stdout("    MT:", buf_mt);
    check_value_dc_stdout("    CF:", buf_cf);

    ierr = vec_dc_delete(&vx);
    ierr = vec_dc_delete(&vy);

    return ierr;
}


int test_ucdotl(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_lc_t vx;
    vec_lc_t vy;

    long double complex buf_st;  /* Single-thread return */
    long double complex buf_mt;  /* Multi-threads return */

    ierr = vec_lc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_lc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_ucdotl(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_ucdotl(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);

    printf("cdotl()\n");
    check_value_lc_stdout("    ST:", buf_st);
    check_value_lc_stdout("    MT:", buf_mt);

    ierr = vec_lc_delete(&vx);
    ierr = vec_lc_delete(&vy);

    return ierr;
}


int test_cdotf(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_fc_t vx;
    vec_fc_t vy;

    float complex buf_st;  /* Single-thread return */
    float complex buf_mt;  /* Multi-threads return */
    float complex buf_cf;  /* cblas return */

    ierr = vec_fc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_fc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_cdotf(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_cdotf(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);
    cblas_cdotc_sub(PROPER_VECTOR_RUN, (void*) vx.data, vx.step, (void*) vy.data, vy.step, (void*) &buf_cf);

    printf("cdotf() -> cblas_cdotc_sub()\n");
    check_value_fc_stdout("    ST:", buf_st);
    check_value_fc_stdout("    MT:", buf_mt);
    check_value_fc_stdout("    CF:", buf_cf);

    ierr = vec_fc_delete(&vx);
    ierr = vec_fc_delete(&vy);

    return ierr;
}


int test_cdot(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_dc_t vx;
    vec_dc_t vy;

    double complex buf_st;  /* Single-thread return */
    double complex buf_mt;  /* Multi-threads return */
    double complex buf_cf;  /* cblas return */

    ierr = vec_dc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_dc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_cdot(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_cdot(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);
    cblas_zdotc_sub(PROPER_VECTOR_RUN, (void*) vx.data, vx.step, (void*) vy.data, vy.step, (void*) &buf_cf);

    printf("cdot() -> cblas_zdotc_sub()\n");
    check_value_dc_stdout("    ST:", buf_st);
    check_value_dc_stdout("    MT:", buf_mt);
    check_value_dc_stdout("    CF:", buf_cf);

    ierr = vec_dc_delete(&vx);
    ierr = vec_dc_delete(&vy);

    return ierr;
}


int test_cdotl(size_t thrz)
{
    int ierr = ERRNO_UNDEFINED;
    vec_lc_t vx;
    vec_lc_t vy;

    long double complex buf_st;  /* Single-thread return */
    long double complex buf_mt;  /* Multi-threads return */

    ierr = vec_lc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vx);
    ierr = vec_lc_create(PROPER_VECTOR_MAX, PROPER_VECTOR_MIN, PROPER_VECTOR_RUN, &vy);

    ierr = stblas_cdotl(PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_st);
    ierr = mtblas_cdotl(thrz, PROPER_VECTOR_RUN, vx.data, vx.step, vy.data, vy.step, &buf_mt);

    printf("cdotl()\n");
    check_value_lc_stdout("    ST:", buf_st);
    check_value_lc_stdout("    MT:", buf_mt);

    ierr = vec_lc_delete(&vx);
    ierr = vec_lc_delete(&vy);

    return ierr;
}
